package com.applications.addressbookapp;

import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class EditContact extends Activity{
	
    // Allows access to data in the EditTexts
	 
    EditText firstName;
    EditText lastName;
    EditText phoneNumber;
    EditText emailAddress;
    EditText homeAddress;
 
    // The database tool class
 
    DBTools dbTools = new DBTools(this);
 
    // Sets up everything when the Activity is displayed
 
    public void onCreate(Bundle savedInstanceState) {
 
        // Get saved data if there is any
 
        super.onCreate(savedInstanceState);
 
        // Designate that edit_contact.xml is the interface used
 
        setContentView(R.layout.edit_contact);
 
        // Get the EditText objects
 
        firstName = (EditText) findViewById(R.id.firstName);
        lastName = (EditText) findViewById(R.id.lastName);
        phoneNumber = (EditText) findViewById(R.id.phoneNumber);
        emailAddress = (EditText) findViewById(R.id.emailAddress);
        homeAddress = (EditText) findViewById(R.id.homeAddress);
 
        // Intent defines that an operation will be performed
 
        Intent theIntent = getIntent();
 
        // Get the extended data provided to this activity
        // putExtra("contactId", contactIdValue); in MainActivity
        // will pass contactId here
 
        String contactId = theIntent.getStringExtra("contactId");
 
        // Get the HashMap of data associated with the contactId
 
        HashMap<String, String> contactList = dbTools.getContactInfo(contactId);
 
        // Make sure there is something in the contactList
 
        if(contactList.size()!=0) {
 
            // Put the values in the EditText boxes
 
            firstName.setText(contactList.get("firstName"));
            lastName.setText(contactList.get("lastName"));
            phoneNumber.setText(contactList.get("phoneNumber"));
            emailAddress.setText(contactList.get("emailAddress"));
            homeAddress.setText(contactList.get("homeAddress"));
 
        }
    }
    
    public void editContact(View view) {
        HashMap<String, String> queryValuesMap =  new  HashMap<String, String>();
 
        // Get the EditText objects
 
        firstName = (EditText) findViewById(R.id.firstName);
        lastName = (EditText) findViewById(R.id.lastName);
        phoneNumber = (EditText) findViewById(R.id.phoneNumber);
        emailAddress = (EditText) findViewById(R.id.emailAddress);
        homeAddress = (EditText) findViewById(R.id.homeAddress);
 
        // Intent defines that an operation will be performed
 
        Intent theIntent = getIntent();
 
        // Get the extended data provided to this activity
        // putExtra("contactId", contactIdValue); in MainActivity
        // will pass contactId here
 
        String contactId = theIntent.getStringExtra("contactId");
 
        // Put the values in the EditTexts in the HashMap
         
        queryValuesMap.put("contactId", contactId);
        queryValuesMap.put("firstName", firstName.getText().toString());
        queryValuesMap.put("lastName", lastName.getText().toString());
        queryValuesMap.put("phoneNumber", phoneNumber.getText().toString());
        queryValuesMap.put("emailAddress", emailAddress.getText().toString());
        queryValuesMap.put("homeAddress", homeAddress.getText().toString());
 
        // Send the HashMap to update the data in the database
         
        dbTools.updateContact(queryValuesMap);
         
        // Call for MainActivity
         
        this.callMainActivity(view);
 
    }
    public  void callMainActivity(View view) {
		Intent objIntent = new Intent(getApplication(),AddressApp.class);
		startActivity(objIntent);
	}

	public void removeContact(View view) {
        Intent theIntent = getIntent();
        String contactId = theIntent.getStringExtra("contactId");
         
        // Call for the contact with the contactId provided
        // to be deleted
         
        dbTools.deleteContact(contactId);
         
        // Call for MainActivity
         
        this.callMainActivity(view);
 
    }
    	

}
